<template>
  <div class="side-bar">
    <div class="tree-menu">
      <!-- 用户定义的数据是否渲染组件 -->
      <template v-for="item of treeMenuData">
        <!-- 有的菜单是没有子项的 -->
        <!-- item.children不存在时才渲染menu-item -->
        <menu-item v-if="!item.children" :key="item.id">
          {{ item.title }}
        </menu-item>

        <!-- 有的菜单是有子项的 -->
        <!-- 再次递归子组件，并传入item到子组件里 -->
        <re-sub-menu :key="item.id" :data="item" v-else> </re-sub-menu>
      </template>
    </div>
  </div>
</template>

<script>
import MenuItem from "./MenuItem.vue";
import ReSubMenu from "./ReSubMenu.vue";

export default {
  name: "TreeMenu",
  components: {
    MenuItem,
    ReSubMenu,
  },
  props: {
    treeMenuData: Array,
  },
};
</script>

<style lang="scss" scoped>
.side-bar {
  width: 200px;

  .tree-menu {
    width: 100%;
    background-color: #000;
    color: #fff;
  }
}
</style>
